﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CauseManage.aspx.cs" Inherits="Education_CauseManage" %>
<%@ Import Namespace="System.Collections.Generic" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>课程管理</title>
    <link href="../Common/css/main.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #load{width:500px;height:25px;border:1px #000 solid;}
        #loading{position:absolute;z-index:2;height:23;filter:progid:DXImageTransform.microsoft.gradient(gradienttype=1,startColorStr=white,endColorStr=#39867b);}
        #loadtext{position:absolute;z-index:3;width:100%;height:100%;line-height:23px;text-align:center;}
    </style>
    
    <script type="text/javascript" src="../Common/Js/jquery.js"></script>

    <script type="text/javascript" src="../Common/Js/common.js"></script>

    <script type="text/javascript" src="../Common/Js/msg.js"></script>
    
    <script type="text/javascript" src="../Common/Js/jsUtil.js"></script>
    
    <script type="text/javascript">
var optionType = '';
var editCauseID = 0;
var queryType = '0';
    
function ShowAddWindow()
{
    optionType = 'add';
 
    //显示添加教师信息对话框
    ShowPopForm("popTable", "formTitle", "添加课程信息");
    //显示背景遮罩
    ShowMask();
    
    //CheckChange(document.getElementById('rightTable').all.selfRight,0);
   // self.parent.frames['left'].activate = 0;
}

function ShowUpdateWindow(obj, intString)
{
    optionType = 'update';
    ShowPopForm("popTable", "formTitle", "修改课程信息");
//    document.getElementById('reBind').style.display = "inline"; //显示‘回复保存’按钮
    var chkGradeObj = $("input[name='grade']");
    var gradearr = new Array();
    for(var i = 0; i < $(chkGradeObj).length; i++) {
        //alert($(chkGradeObj).eq(i).attr("value"));
        var chkOne =  $(chkGradeObj).eq(i);
        if(intString.indexOf($(chkOne).attr("value") + ",") != -1)
            $(chkOne).attr("checked", "true");
        else
            $(chkOne).removeAttr("checked");
            
    }

    ShowMask();
    //self.parent.frames['left'].activate = 0;
    var causeId = obj.id;
    editCauseID = causeId;

    PageMethods.SelectCause(causeId,function(succ){       
        if(succ.length <=0)
            return;
        
        document.getElementById('causeName').value = succ.CauseName;
        document.getElementById('causeName').setAttribute("oldCode",succ.CauseName);
       

    },function(err){//异步失败
        showMassgeBox("提示",CAUSE_QUERY_FAIL,1,null);
    }) 
}


function DeleteCause(obj)
{
    showConfirmBox("删除", "确定将该课程删除?", DelCauseInfo, obj);
}
//删除课程信息
function DelCauseInfo(obj)
{
    var arr = new Array();
    var causeId = obj.id;

   //判断联系表里面是否有这门课程，如果有，则提示不能删除，没有的话，就删除
   PageMethods.GetUseCountByCauseID(causeId, function(succ)
   {
        if (succ>0)
        {
            showMassgeBox("提示",CAUSE_USED_FAIL,1,null);
        }
        else
        {
            PageMethods.DeleteCauseInfo(causeId, function(succ)
            {
                 if (succ>0)
                 {                    
                    Bind(0,"");     
                    showMassgeBox("提示",CAUSE_DEL_SUCC,1,null);              
                 } 
                 else if(succ == 0)
                 {
                    showMassgeBox("提示",CAUSE_DEL_FAIL,1,null);
                 }      
                 else
                 {
                    showMassgeBox("提示",CAUSE_DEL_FAIL1,1,null);
                 }
            },function(err){
                showMassgeBox("提示",CAUSE_DEL_FAIL,1,null);
            })
        }   
   })   

}

//上移课程编号
function UpCauseNo(obj)
{
    var CauseID = obj.id;
     PageMethods.MoveCauseNO(CauseID, -1,function(succ){
            if (succ > 0)
            {
                Bind(0,"");                
            }
            else if (succ == -2)
            {
                showMassgeBox("提示","已经在编号最顶端，不能再向前调整",1,null);
                return;
            }
            else
                showMassgeBox("提示","调整失败",1,null);
     },function(err){
        showMassgeBox("提示","调整失败",1,null);
     })
	
}

//下移课程编号
function DownCauseNo(obj)
{
	 var CauseID = obj.id;
     PageMethods.MoveCauseNO(CauseID, 1,function(succ){
            if (succ > 0)
            {

                Bind(0,"");                 
            }
            else if (succ == -1)
            {
                showMassgeBox("提示","已经在编号最尾端，不能再向后调整",1,null);
                return;
            }
            else
                showMassgeBox("提示","调整失败",1,null);
     },function(err){
        showMassgeBox("提示","调整失败",1,null);
     })
}

function ShowMask()
{
    showMask();
    //self.parent.frames['left'].showMask();
}

function CloseMask(formId)
{
    //self.parent.frames['left'].activate = 1;
    editCauseID = 0;
    
    ReleasePopTable();
    ClosePopForm(formId,true);
    hideMask();
    //self.parent.frames['left'].hideMask();
}

//添加或更新操作成功后清空内容
function ReleasePopTable()
{
    document.getElementById('causeName').value = ""
}

function Save()
{
    var causeName = document.getElementById('causeName').value.Trim();   
    
    if (causeName == "")
    {
        showNoticMsg('causeName',CAUSE_NAME_IS_NULL);
        document.getElementById('causeName').select();  
        return;
    } 
    
    var iValid = containSpecialChar(causeName);
    if (iValid == true)
    {
        showNoticMsg('causeName',CAUSE_NAME_WRONG);
        document.getElementById('causeName').select();  
        return;
    }
    
    if (optionType == "add")
    {
        Add(causeName);
    }
    if (optionType == "update")
    {
        Update(causeName);
    }
}

function Add( causeName)
{
    var chkGradeObj = $("input[name='grade'][checked]");
    var gradearr = new Array();
    for(var i = 0; i < $(chkGradeObj).length; i++) {
        //alert($(chkGradeObj).eq(i).attr("value"));
        gradearr.push($(chkGradeObj).eq(i).attr("value"));
    }
    var cause = {RID:0,CauseNo:0,CauseName:causeName};
    PageMethods.ValidateUsed(cause,function(succ){ 
           if(succ > 0)
           {
                if (succ == 1)
                {
                    showNoticMsg('causeName','该课程已存在，请重新输入'); 
                    document.getElementById('causeName').select();   
                    return;
                }
                
                if (succ == 2)
                {
                    showNoticMsg('causeName','该课程已作废，请重新输入'); 
                    document.getElementById('causeName').select();   
                    return;
                }
           }
           else
           {  
                PageMethods.InsertCauseInfo(cause, gradearr, function(succ)
                {
                    if (succ > 0)
                    {
                        Bind(0,"");   
                        CloseMask('popTable');
                    }
                    else
                    {
                        showMassgeBox("提示",CAUSE_INSERT_FAIL,1,null);
                    } 	
                },function(err){
                    showMassgeBox("提示",CAUSE_INSERT_FAIL,1,null);
                })
           }
        },function(err){
             showMassgeBox("提示",CAUSE_FAIL,1,null);
        });    
}

function Update(causeName)
{   
    var chkGradeObj = $("input[name='grade'][checked]");
    var gradearr = new Array();
    for(var i = 0; i < $(chkGradeObj).length; i++) {
        //alert($(chkGradeObj).eq(i).attr("value"));
        gradearr.push($(chkGradeObj).eq(i).attr("value"));
    }

    var cause = {RID:editCauseID,CauseNo:0,CauseName:causeName}
    var oldCauseName = document.getElementById('causeName').getAttribute('oldCode');
    //判断课程编号或课程称是否修改，先验证后更新
    if ( oldCauseName != causeName )
    {
        PageMethods.ValidateUsed(cause,function(succ){ 
           if(succ > 0)
           {
                if (succ == 1)
                {
                    showNoticMsg('causeName','该课程已存在，请重新输入'); 
                    document.getElementById('causeName').select();   
                    return;
                }
                
                if (succ == 2)
                {
                    showNoticMsg('causeName','该课程已作废，请重新输入'); 
                    document.getElementById('causeName').select();   
                    return;
                }
           }
           else
           {  
                PageMethods.UpdateCauseInfo(cause, gradearr, function(succ)
                {
                    if (succ > 0)
                    {
                        Bind(0,"");   
                        CloseMask('popTable');
                    }
                    else
                    {
                        showMassgeBox("提示",CAUSE_UPDATE_FAIL,1,null);
                    } 	
                },function(err){
                    showMassgeBox("提示",CAUSE_UPDATE_FAIL,1,null);
                })
           }
        },function(err){
             showMassgeBox("提示",CAUSE_FAIL,1,null);
        });
    }
    //没有修改的情况直接更新
    else
    {
        PageMethods.UpdateCauseInfo(cause, gradearr, function(succ)
        {
              if (succ > 0)
              {
                  Bind(0,"");   
                  CloseMask('popTable');
              }
              else
              {
                  showMassgeBox("提示",CAUSE_UPDATE_FAIL,1,null);
              } 	
         },function(err){
              showMassgeBox("提示",CAUSE_UPDATE_FAIL,1,null);
       });
    }
}

function Bind(queryType,queryValue)
{
    if (queryType == 0)
    {
        document.getElementById('SelectType').value = "0";
        document.getElementById('selectText').value = "";
        document.getElementById('selectText').disabled = true;
    }

    //查询条件
    document.getElementById('HFQueryType').value = queryType;
    document.getElementById('HFQueryValue').value = queryValue;
    document.getElementById('btnBind').click();
}

function Load()
{
    document.getElementById('selectText').disabled = true;
    Bind(0,"");
}

function QueryCause()
{
    if (queryType == '0')
    {
       Bind(0,"");
    }
    
    if (queryType == '1')
    {
        var causeNo = document.getElementById('selectText').value.Trim();
        
        if (containSpecialChar(causeNo))
        {
            showNoticMsg('selectText',"查询内容不能包含特殊字符", -150);
            return;
        }
        
        if (causeNo == "")
        {
            Bind(0,"");
            return;
        }
        if (!isNumber(causeNo))
        {
            showNoticMsg('selectText',"请输入整数类型的课程编号", -150);
            return;
        }
        Bind(1,causeNo);
    }

    if (queryType == '2')
    {
        var causeName = document.getElementById('selectText').value.Trim();
        
        if (containSpecialChar(causeName))
        {
            showNoticMsg('selectText',"查询内容不能包含特殊字符", -150);
            return;
        }
        
        if (causeName == "")
        {
             Bind(0,"");
            return;
        }
        
         Bind(2,causeName);
    }
}

function changeSelectTypeList(obj)
{
    if (obj.value == '0')
    {
        document.getElementById('selectText').disabled = true;
    }
    else if (obj.value == '1')
    {
        document.getElementById('selectText').disabled = false;
    }
    else if (obj.value == '2')
    {
        document.getElementById('selectText').disabled= false;
    }
    
    document.getElementById('selectText').value = "";   
    queryType = obj.value;
}

function submitQuery()
{
    if(event.keyCode==13)     
    {     
        event.keyCode=9;   
        event.returnValue = false;
        document.getElementById('Button1').click();     
    }   
}

function SetHightLight(obj)
{
 obj.style.backgroundColor = "#fffccf";
 var table = document.getElementById('Table1');
 for(var i =0 ; i < table.rows.length;i++)
 {
    var row = table.rows[i];
    if(row != null && row.id != obj.id)
        row.style.backgroundColor = "#ffffff";
    
 }
}

    </script>
</head>
<body onload="Load();">
    <div class="mainWarp">
        <div class="pageBody">
            <form id="form1" runat="server"> 
                <div>  
                    <ul class="butList">
                    <li>        
                    <table width="100%" border="0">
                        <tr>
                            <td  width= "40%" style ="text-align: left;">
                                <input type="button" id="button3" value="新增" onclick="ShowAddWindow();" class="commonBut" />
                        </td>
                        <td idth= "60%" style ="text-align: right;">
                            查询方式:<select id="SelectType" runat="server" style="width: 120px" onchange="changeSelectTypeList(this);">
                                <option value='0'>所有</option>
                                <option value='1'>课程编号</option>
                                <option value='2'>课程名称</option>
                            </select> 
                            <input id="selectText" type="text"  onkeydown="submitQuery();" class="txtStyle" style="width: 120px"/>
                            <input id="Button1" type="button" value="查询" onclick="QueryCause();" class="commonBut"/>
                        </td>
                        </tr>
                    </table>
                    </li>
                    </ul>
                </div> 
                <div>
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
                    </asp:ScriptManager>
                </div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Repeater ID="Repeater1" runat="server">
                            <HeaderTemplate>
                                <table id="Table1" border="0" cellpadding="0" cellspacing="1" class="useList">
                                    <tr class="tableTitle">
                                        <th width="10%" style="text-align:center;padding-left:5px;">课程编号</th>
                                        <th width="40%" style="text-align:center;padding-left:5px;">课程名称</th>
                                        <th width="20%" style="text-align:center;padding-left:5px;">年级</th>
                                        <th width="30%" ></th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr id="<%#DataBinder.Eval(Container.DataItem,"RID")%>" style="background-color:White;" onclick="SetHightLight(this);">                                   
                                    <td name="CauseNo" style="text-align:right; font-weight:normal;padding-left:5px;"> 
                                         <%# DataBinder.Eval(Container.DataItem, "CauseNo")%>
                                    </td>
                                    <td name="CauseName" style="text-align:center; font-weight:normal;padding-left:5px;"> 
                                        <%# DataBinder.Eval(Container.DataItem, "CauseName")%>
                                    </td>
                                    <td name="Grade" style="text-align:center; font-weight:normal;padding-left:5px;"> 
                                        <%# (DataBinder.Eval(Container.DataItem, "GradeString"))%>
                                    </td>
                                    <td>
                                        <img src="../common/images/hadle_icon.gif" alt="修改课程信息" class="imageBtnStyle" onclick="ShowUpdateWindow(this.parentElement.parentElement,  '<%# (DataBinder.Eval(Container.DataItem, "GradeIntString"))%>')" />
                                        <img src="../common/images/delete_icon.gif" alt="删除课程信息" class="imageBtnStyle" onclick="DeleteCause(this.parentElement.parentElement);" />
                                        <img src="../common/images/arrow_up.gif" alt="上移" class="imageBtnStyle" onclick="UpCauseNo(this.parentElement.parentElement);" />
                                        <img src="../common/images/arrow_down.gif" alt="下移" class="imageBtnStyle" onclick="DownCauseNo(this.parentElement.parentElement);" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                             </FooterTemplate>
                        </asp:Repeater>
                        <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Right"  PageSize="15"  width="100%"   PagingButtonSpacing="10px" ButtonImageAlign="left" OnPageChanged="AspNetPager1_PageChanged"> </webdiyer:aspnetpager> 
                    </ContentTemplate>
                     <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnBind" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>  
                <div style="display: none;">
                    <asp:Button ID="btnBind" runat="server" OnClick="btnBind_Click" Text="btnBind" />
                    <asp:HiddenField ID="HFQueryType" runat="server" Value="" />
                    <asp:HiddenField ID="HFQueryValue" runat="server" Value="" />
                &nbsp;
                </div>
            </form>            
            <div id="popTable" class="layoutWarp" style="display: none; width: 300px; overflow: inherit;">
                <div class="title">
                    <span id="formTitle">新增</span> <a title="关闭" onclick="return CloseMask('popTable')">
                        关闭</a>
                </div>
                <table id="tableCauseObject" style="width: 280px; margin-top: 12px;" border="0" class="layoutTable">
                    <tr>
                        <td align='right'>课程名称:</td>
                        <td style="text-align: left;color:Red"><input type="text" style="width: 120px" id="causeName" maxlength="10" class="txtStyle" /> *</td>                    
                    </tr>
                    
                    <tr>
                        <td colspan="3" align="center">
                        <input type="checkbox" name="grade" value="7" />初一
                        <input type="checkbox" name="grade" value="8"/>初二
                        <input type="checkbox" name="grade" value="9"/>初三
                        </td>
                    </tr>
                    
                     <tr>
                        <td colspan="3" align="center">
                            <ul class="butList">
                                <input type="button" id="btnSave" value="保存" onclick="Save()" class="commonBut" />
                                <input type="button" value="取消" onclick=" CloseMask('popTable');" class="commonBut" />
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>                 
        </div>
    </div>
</body>
</html>
